//
//  BwtServiceAd.m
//  bwt_ad_flutter
//
//  Created by 帅 on 2025/6/24.
//

#import "BwtServiceAd.h"
#import "BwtAdEventManager.h"
#import "BwtAdLogManager.h"
@interface BwtServiceAd()<CXHSDKServiceAdViewDelegate>
@end
@implementation BwtServiceAd



- (void)loadBannerAd{
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    UIViewController *rvc = window.rootViewController;
    _serviceAdView = [[CXHAdSDKServiceAdView alloc] initWithFrame:CGRectMake(0, 0, _adParams.adWidth.floatValue, _adParams.adHeight.floatValue)];
    
    _serviceAdView.delegate = self;
    _serviceAdView.controller = rvc;
    _serviceAdView.refershTime = 35;
    _serviceAdView.posId = _adParams.posId;
    _serviceAdView.showCloseView = _adParams.showClose;
    _serviceAdView.backgroundColor = [UIColor clearColor];
    // 查看价格，需要在设置广告位id之后调用
//    NSLog(@"price----%ld", [_serviceAdView getBidPrice]);
    // 发送竞价失败上报，此时的价格为第三方赢价的价格
    // [_serviceAdView sendLossNotice:100];
    // 发送竞价成功上报
//    [_serviceAdView sendWinNotice:100];
    // 2 添加到父视图上
//    _serviceAdView.frame = CGRectMake(0, 200, 350, 150);
//    [rvc.view addSubview:_serviceAdView];
    // 3 加载并显示广告
    [_serviceAdView loadAndShow];
    
}
/**
广告获取成功
@param serviceAdView 广告实例
*/
- (void)CXH_serviceAdViewDidReceived:(CXHAdSDKServiceAdView *)serviceAdView{
    // 3 加载并显示广告 注意: 请确保banner视图显示在屏幕内的时候,调用load方法
    BwtAdEventManager *bwtAdEventManager = [BwtAdEventManager getBwtAdEventManager];

    [bwtAdEventManager postOnAdLoadEvent:_adParams];
}

/**
广告拉取失败
@param serviceAdView 广告实例
@param error 错误描述
*/
- (void)CXH_serviceAdViewFailToReceived:(CXHAdSDKServiceAdView *)serviceAdView
error:(NSError *)error{
    NSString *message = @"广告渲染失败了";
    dispatch_async(dispatch_get_main_queue(), ^{
        BwtAdEventManager *bwtAdEventManager = [BwtAdEventManager getBwtAdEventManager];
        [bwtAdEventManager postOnAdLoadFailedEvent:self.adParams message:message];
        [self releaseAd];
    });
   
}


/**
广告点击
@param serviceAdView 广告实例
@param loadingPageURL 广告落地页地址，当渠道为bwt，并且customLoadingPage为YES时有值
*/
- (void)CXH_serviceAdViewClicked:(CXHAdSDKServiceAdView *)serviceAdView
loadingPageURL:(NSString *)loadingPageURL{
    [[BwtAdLogManager getBwtAdLogManager] log:@"服务类型广告被点击, adId: %@", _adParams.adId];
    /// 通知Flutter端
    BwtAdEventManager *bwtAdEventManager = [BwtAdEventManager getBwtAdEventManager];
    [bwtAdEventManager postOnAdClickEvent:_adParams];
}


/**
广告关闭
@param serviceAdView 广告实例
 */
- (void)CXH_serviceAdViewClose:(CXHAdSDKServiceAdView *)serviceAdView{
    
    dispatch_async(dispatch_get_main_queue(), ^{
        [[BwtAdLogManager getBwtAdLogManager] log:@"banner广告被关闭, adId: %@", self.adParams.adId];
        /// 通知Flutter端
        BwtAdEventManager *bwtAdEventManager = [BwtAdEventManager getBwtAdEventManager];
        [bwtAdEventManager postOnAdCloseEvent:self.adParams];
        [self releaseAd];
    });
}

/**
广告展示
@param serviceAdView 广告实例
*/
- (void)CXH_serviceAdViewExposure:(CXHAdSDKServiceAdView *)serviceAdView{
    [[BwtAdLogManager getBwtAdLogManager] log:@"banner广告被展示, adId: %@", _adParams.adId];
    /// 通知Flutter端
    BwtAdEventManager *bwtAdEventManager = [BwtAdEventManager getBwtAdEventManager];
    [bwtAdEventManager postOnAdExposeEvent:_adParams];
}


- (void)releaseAd{
    [_serviceAdView removeFromSuperview];
    _serviceAdView = nil;
}

@end
